<script setup lang="ts">
import { writeFile, utils } from "xlsx";

function exportExcel() {
  let arr: any = [];
  for (let i = 0; i < 100000; i++) {
    arr.push({
      id: i,
      name: `大壮${i}号`,
      location: `xxx大道${i}号`,
      age: i,
      a: i * 2,
      b: i / 2,
      c: i + 2,
      d: 22,
      e: 222,
      f: 234,
    });
  }
  const sheet = utils.json_to_sheet(arr);
  const workbook = utils.book_new();
  utils.book_append_sheet(workbook, sheet, "Sheet1");
  console.log(workbook);
  writeFile(workbook, "test.xlsx");
}

// 优化
const worker1 = new Worker("http://localhost:5173/excelworker.js");
worker1.addEventListener("message", (e) => {
  console.log(e.data);
  writeFile(e.data, "test.xlsx");
});
const exportExcelTurbo = () => {
  worker1.postMessage("1");
};
</script>

<template>
  <div class="">
    <h1>Example2——excel导出</h1>
    <input type="text" />
    <button @click="exportExcel">导出</button>
    <button @click="exportExcelTurbo">导出-优化</button>
  </div>
</template>

<style lang="scss" scoped></style>
